#!/usr/bin/perl

######################################################################
package Swignition::uF::COinS;
######################################################################

# COinS isn't really a microformat, but it's close enough for our purposes.

use XML::LibXML qw(:all);
use strict;
use CGI;
use Swignition::Export;

sub consume
{
	my $page        = shift;
	my $pageURL     = $page->uri;
	my $pageSubject = $page->{RDF}->{$pageURL};
	
	my $NS = 'http://www.openurl.info/registry/fmt/xml/rss10/ctx#';
	
	my @coins = Swignition::GenericParser::Utils::searchClass('Z3988', $page->{DOM});
	push @coins, Swignition::GenericParser::Utils::searchRel('Z3988', $page->{DOM});
	
	foreach my $node (@coins)
	{
		my $openurl;
	
		if (lc($node->tagName) eq 'a')
		{
			$openurl =  $node->getAttribute('href');
			$openurl =~ s/^([^\?]*\?)//;
		}
		elsif ($node->tagName =~ /^(span|cite)$/i)
		{
			$openurl =  $node->getAttribute('title');
		}
		elsif ($node->tagName =~ /^(q|blockquote)$/i)
		{
			$openurl =  $node->getAttribute('cite');
			$openurl =~ s/^([^\?]*\?)//;
		}
		else
		{
			next;
		}
		
		my $cgi = new CGI($openurl);
		next unless ($cgi->param('ctx_ver') eq 'Z39.88-2004');
		
		my $subject;
		if (length $node->getAttribute('href'))
		{
			$subject = Swignition::RDFModel::Subject->new(
				$page->uri($node->getAttribute('href')));
			$subject->addUri("http://openurl.invalid/?$openurl");
		}
		elsif (length $node->getAttribute('id'))
		{
			$subject = Swignition::RDFModel::Subject->new(
				$pageURL . '#' . $node->getAttribute('id'));
			$subject->addUri("http://openurl.invalid/?$openurl");
		}
		else
		{
			$subject = Swignition::RDFModel::Subject->new(
				"http://openurl.invalid/?$openurl");
		}
		
		foreach my $param ( $cgi->param )
		{
			my $val = $cgi->param($param);
			$subject->addVal($NS.$param, Swignition::RDFModel::Value->new($val));
		}
		
		my $Quote;
		if ($node->tagName =~ /^(q|blockquote)$/i)
		{
			my $QuoteText = Swignition::RDFModel::Value->new(
				Swignition::GenericParser::Utils::XML_STRINGIFY($node));
			$QuoteText->setDatatype('http://www.w3.org/1999/02/22-rdf-syntax-ns#XMLLiteral');
			$QuoteText->{xmlns} = 'http://www.w3.org/1999/xhtml';
			$Quote = Swignition::RDFModel::Subject->new(
				Swignition::GenericParser::Utils::BNodeURI($node, 'Quotation')
			);
			$Quote->addVal('http://purl.org/dc/terms/description', $QuoteText);
			$Quote->addVal(
				'http://purl.org/dc/terms/source', 
				Swignition::RDFModel::Value->new($subject->uri, 1)
				);
			# DocModel.pm now does this more accurately:
			# $subject->addVal('http://purl.org/dc/terms/hasPart',
			#	Swignition::RDFModel::Value->new($Quote->uri, 1));
		}
		elsif ($node->tagName =~ /^(cite|a)$/i)
		{
			my $label = Swignition::GenericParser::Utils::STRINGIFY($node);
			$subject->addVal('http://www.w3.org/2000/01/rdf-schema#label',
				Swignition::RDFModel::Value->new($label)) if (length "$label");
		}
		# else: <span>. Note that with SPAN we ignore the contents.
		
		$subject->addVal('http://www.w3.org/1999/02/22-rdf-syntax-ns#type',
			Swignition::RDFModel::Value->new($NS.'object', 1));
		$pageSubject->addVal('http://purl.org/dc/terms/bibliographicCitation',
			Swignition::RDFModel::Value->new($subject->uri, 1));
		
		$page->mergeSubject($subject);
		$page->mergeSubject($Quote) if ($Quote);
	}
	
	$page->mergeNS('ctx', $NS, 'RSS Context Module (OpenURL)');
}


1;
